package com.fengze.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fengze.business.service.IEarlyStageProjectService;
import com.fengze.business.vo.EarlyStageProjectVO;
import com.fengze.business.vo.HistoryProduct;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fengze.common.annotation.Log;
import com.fengze.common.core.controller.BaseController;
import com.fengze.common.core.domain.AjaxResult;
import com.fengze.common.enums.BusinessType;
import com.fengze.business.domain.Products;
import com.fengze.business.service.IProductsService;
import com.fengze.common.utils.poi.ExcelUtil;
import com.fengze.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品信息Controller
 * 
 * @author lg
 * @date 2024-01-31
 */
@RestController
@RequestMapping("/business/products")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;
    @Autowired
    private IEarlyStageProjectService earlyStageProjectService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(Products products)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }


    /**
     * 查询产品信息列表
     */
    @GetMapping("/history/list")
    public TableDataInfo historyList(Products products)
    {
        startPage();
        List<HistoryProduct> list = productsService.selectProductsHistoryList(products);
        for (int i = 0; i < list.size(); i++) {
            HistoryProduct historyProduct = list.get(i);
            Long earlyStageProjectId = historyProduct.getEarlyStageProjectId();
            if (ObjectUtils.isEmpty(earlyStageProjectId)) {
                continue;
            }
            EarlyStageProjectVO earlyStageProjectVO = earlyStageProjectService.selectEarlyStageProjectVOById(earlyStageProjectId);
            historyProduct.setCode(earlyStageProjectVO.getCode());
        }
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:products:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:products:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productsService.selectProductsById(id));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:products:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(Products products, MultipartFile imgFile)
    {
        productsService.insertProductsWithImg(products,imgFile);
        return toAjax(1);
    }


    @PreAuthorize("@ss.hasPermi('business:products:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping("/addInfo")
    public AjaxResult addInfo(@RequestBody Products products)
    {
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:products:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(Products products,MultipartFile imgFile)
    {
        return toAjax(productsService.updateProductsWithFile(products,imgFile));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:products:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productsService.deleteProductsByIds(ids));
    }
}
